import { ref } from 'vue'
import type { ConsultOrderItem } from '@/types/consult'
import { cancelOrderById } from '@/services/consult'
import { showSuccessToast, showFailToast } from 'vant'
import { OrderType } from '@/enums'

export function useCancelConsult(orderItem: ConsultOrderItem) {
  const loading = ref<boolean>(false)
  const handleCancelConsult = async () => {
    // 让取消的loading显示
    loading.value = true
    // 发请求
    try {
      await cancelOrderById(orderItem.id)
      orderItem.status = OrderType.ConsultCancel
      orderItem.statusValue = '已取消'
      showSuccessToast('取消成功')
    } catch (error) {
      showFailToast('取消失败')
    } finally {
      // 成功之后，让取消的loading隐藏，弹提示
      loading.value = false
    }
  }
  return {
    loading,
    handleCancelConsult
  }
}
